package com.bj58.groupbuy.action.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.service.UserManagerService;
import com.bj58.groupbuy.service.locator.ServiceLocator;
import com.bj58.groupbuy.util.CookieParser;
import com.bj58.wf.dal.annotation.Column;

/**公用方法模块
 * @author Administrator
 *
 */
public class SomePublicUtil {
	private UserManagerService userManagerService =  ServiceLocator.get(UserManagerService.class);
	
	/** 通过cookie获取当前用户信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public  User  getUserByCookie(HttpServletRequest request) throws Exception{
		CookieParser cookieParser = new CookieParser(request.getHeader("Cookie"));
		String param = cookieParser.getString("t58com_action_user", "");
		String str[] =  param.split("_");
		User user = userManagerService.getUserByMd5(str[0]);
		return user;
	}
	
	
	/** 通过session获取当前用户信息
	 * @param request
	 * @return
	 * @throws Exception
	 */
	public   User  getUserBySession(HttpServletRequest request) throws Exception{
		HttpSession session=request.getSession();
		String username=(String)session.getAttribute("username");
		User user = userManagerService.getUserByName(username);
		return user;
	}

	
	/**通过Column注解获取vo字段中英文字典，以,号隔开（英,中)
	 * @param <T>
	 * @param clazz
	 * @return
	 */
	public <T>List<String>  getECDicByAnnotation(Class<T> clazz){
		List<String>  rsList = new ArrayList<String>();
		Field[] fds = clazz.getDeclaredFields();
        for (Field fd : fds) {  
            /* 
             * 判断方法中是否有指定注解类型的注解 
             */  
            boolean hasAnnotation = fd.isAnnotationPresent(Column.class);  
            if (hasAnnotation) {  
                /* 
                 * 根据注解类型返回方法的指定类型注解 
                 */  
            	Column annotation = fd.getAnnotation(Column.class);
            	rsList.add(fd.getName()+","+annotation.value());
            }  
        }  
        return rsList;
	}
	
	public  static void emailWarn(String content){
		String title = "Error!!~"+ DateUtil.getStringDate(new Date(), "yyyy-MM-dd HH:mm");
		MailUtil.javaMailSend(title, content, "huangliang@58.com;zhaoxiang@58.com",null);
    	Map<String,String> telMap=new HashMap<String,String>();
    	telMap.put("13522482231","Error:"+content);
    	telMap.put("13811583416","Error:"+content);
    	SubmitSms.submitMsg(telMap);
	}
	
	
	public  static void emailWarnHL(String content){
		String title = "Error!!~"+ DateUtil.getStringDate(new Date(), "yyyy-MM-dd HH:mm");
		MailUtil.javaMailSend(title, content, "huangliang@58.com",null);
    	Map<String,String> telMap=new HashMap<String,String>();
    	telMap.put("13811583416","Error:"+content);
    	SubmitSms.submitMsg(telMap);
	}
	
	
	public  static void emailWarnRealTime(String content){
		String title = "Warn!!~"+ DateUtil.getStringDate(new Date(), "yyyy-MM-dd HH:mm");
		MailUtil.javaMailSend(title, content, "huangliang@58.com;lihao06@58.com;lichao08@58.com;",null);
    	Map<String,String> telMap=new HashMap<String,String>();
    	telMap.put("13811583416","实时数据预警:"+content);
    	telMap.put("18510330020","实时数据预警:"+content);
    	telMap.put("15011226135","实时数据预警:"+content);
    	SubmitSms.submitMsg(telMap);
	}
}

